package org.msfj.admin.org.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.msfj.admin.model.User;
import org.msfj.admin.model.User;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.DeptService;
import org.msfj.admin.org.service.UserService;
import org.msfj.admin.vo.UserVo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseController;
import org.msfj.common.context.UserContextHandler;
import org.msfj.common.model.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 系统用户信息controller
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@Api(value = "系统用户信息接口", tags = "系统用户信息接口")
@RestController
@RequestMapping("org/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    /**
     * 添加系统用户信息
     *
     * @param userVo
     * @return CommonResult.ok(UserVo)
     */
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加系统用户信息", notes = "添加系统用户信息")
    @ApiImplicitParam(value = "userVo", name = "userVo", dataType = "org.msfj.admin.vo.UserVo", paramType = "query")
    public CommonResult addUser(UserVo userVo) {
        UserVo user = userService.getUserByAccount(userVo.getAccount());
        if (ObjectUtil.isNotNull(user)) {
            return CommonResult.error("此邮箱已被使用");
        }
        user = userService.getUserByEmpNo(userVo.getEmpNo());
        if (ObjectUtil.isNotNull(user)) {
            return CommonResult.error("当前工号已存在");
        }
        userVo.setPassword(userService.encryptPassword(userVo.getPassword()));
        User user1 = userService.saveOrEdit(userVo);
        return CommonResult.ok(userService.setOtherProperties(user1));
    }

    /**
     * 修改系统用户信息
     *
     * @param userVo
     * @return CommonResult.ok(UserVo)
     */
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "修改系统用户信息", notes = "修改系统用户信息")
    @ApiImplicitParam(value = "userVo", name = "userVo", dataType = "org.msfj.admin.vo.UserVo", paramType = "query")
    public CommonResult editUser(UserVo userVo) {
        User old = userService.getById(userVo.getId());
        if (ObjectUtil.isNull(old)) {
            return CommonResult.error("用户不存在");
        }
        String oldPassword = userVo.getOldPassword();
        if (StrUtil.isNotBlank(oldPassword)
                && !new BCryptPasswordEncoder(Constants.PW_ENCORDER_SALT).matches(oldPassword, old.getPassword())) {
            return CommonResult.error("旧密码输入有误");
        }
        if (StrUtil.isNotBlank(userVo.getPassword())) {
            userVo.setPassword(userService.encryptPassword(userVo.getPassword()));
        } else {
            userVo.setPassword(old.getPassword());
        }
        User user = userService.saveOrEdit(userVo);
        return CommonResult.ok(userService.setOtherProperties(user));
    }

    /**
     * 逻辑删除系统用户信息
     *
     * @param userVo
     * @return CommonResult.ok(UserVo)
     */
    @PostMapping("/del")
    @ResponseBody
    @ApiOperation(value = "逻辑删除系统用户信息", notes = "逻辑删除系统用户信息")
    @ApiImplicitParam(value = "userVo", name = "userVo", dataType = "org.msfj.admin.vo.UserVo", paramType = "query")
    public CommonResult delUser(UserVo userVo) {
        User user = new User();
        user.setId(userVo.getId());
        User user1 = userService.delById(user);
        return CommonResult.ok(userService.setOtherProperties(user1));
    }

    /**
     * 逻辑删除系统用户信息
     *
     * @param ids
     * @return CommonResult.ok()
     */
    @PostMapping("/dels")
    @ResponseBody
    @ApiOperation(value = "逻辑删除系统用户信息", notes = "逻辑删除系统用户信息")
    @ApiImplicitParam(value = "ids", name = "ids", dataType = "java.util.List", paramType = "query")
    public CommonResult delUsers(@RequestParam("ids[]") String[] ids) {
        User user = new User();
        userService.delAllByIds(ids, user);
        return CommonResult.ok();
    }

    /**
     * 真实删除系统用户信息
     *
     * @param userVo
     * @return CommonResult.ok()
     */
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "真实删除系统用户信息", notes = "真实删除系统用户信息")
    @ApiImplicitParam(value = "userVo", name = "userVo", dataType = "org.msfj.admin.vo.UserVo", paramType = "query")
    public CommonResult deleteUser(UserVo userVo) {
        userService.trueDelById(userVo);
        return CommonResult.ok();
    }

    /**
     * 根据id查找系统用户信息
     *
     * @param userVo
     * @return CommonResult.ok(UserVo)
     */
    @GetMapping("/get")
    @ResponseBody
    @ApiOperation(value = "根据id查找系统用户信息", notes = "根据id查找系统用户信息")
    @ApiImplicitParam(value = "userVo", name = "userVo", dataType = "org.msfj.admin.vo.UserVo", paramType = "query")
    public CommonResult getUser(UserVo userVo) {
        User user = userService.getById(userVo.getId());
        return CommonResult.ok(userService.setOtherProperties(user));
    }

    /**
     * 查询当前系统用户信息
     *
     * @return
     */
    @GetMapping("/getMyProfile")
    @ResponseBody
    @ApiOperation(value = "查询当前系统用户信息", notes = "查询当前系统用户信息")
    public CommonResult getMyProfile() {
        User user = userService.getById(UserContextHandler.getUserID());
        return CommonResult.ok(userService.setOtherProperties(user));
    }

    /**
     * 根据翻页参数、搜索参数查找系统用户信息
     *
     * @param userVo
     * @param page   当前第几页
     * @param rows   每页多少条记录
     * @return CommonResult.ok(PageInfo<UserVo>)
     */
    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value = "根据翻页参数、搜索参数查找系统用户信息", notes = "根据翻页参数、搜索参数查找系统用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userVo", name = "userVo", dataType = "org.msfj.admin.vo.UserVo", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "page", dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "rows", value = "rows", dataType = "Integer", paramType = "path")
    })
    public CommonResult findUserList(UserVo userVo, Integer page, Integer rows) {
        PageInfo<UserVo> userVoPageInfo = userService.getByUser(userVo, page, rows);
        return CommonResult.ok(userVoPageInfo);
    }

    /**
     * 根据搜索参数查找所有系统用户信息
     *
     * @param userVo
     * @return CommonResult.ok(List<UserVo>)
     */
    @GetMapping("/getAll")
    @ResponseBody
    @ApiOperation(value = "根据搜索参数查找所有系统用户信息", notes = "根据搜索参数查找所有系统用户信息")
    @ApiImplicitParam(value = "userVo", name = "userVo", dataType = "org.msfj.admin.vo.UserVo", paramType = "query")
    public CommonResult getAllUser(UserVo userVo) {
        List<UserVo> userVoList = userService.getAllUser(userVo);
        return CommonResult.ok(userVoList);
    }

    /**
     * 查询用户表拼接部门名称与用户，返回Map
     *
     * @param userVo
     * @return
     */
    @GetMapping("/getDeptNameAndUsers")
    @ResponseBody
    @ApiOperation(value = "查询用户表拼接部门名称与用户，返回Map", notes = "查询用户表拼接部门名称与用户，返回Map")
    @ApiImplicitParam(value = "userVo", name = "userVo", dataType = "org.msfj.admin.vo.UserVo", paramType = "query")
    public CommonResult getUsersByDeptId(UserVo userVo) {
        Map<String, List<UserVo>> usersByDeptId = userService.getUsersByDeptIds(userVo);
        return CommonResult.ok(usersByDeptId);

    }
}
